/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.dao.ProductCategoryDao;
import com.hboxs.asl.entity.ProductCategory;
import com.hboxs.asl.service.ProductCategoryService;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * Service - 商品分类
 */
@Service("productCategoryServiceImpl")
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategory, Long> implements ProductCategoryService {

    @Resource(name = "productCategoryDaoImpl")
    private ProductCategoryDao productCategoryDao;

    @Resource(name = "productCategoryDaoImpl")
    public void setBaseDao(ProductCategoryDao productCategoryDao) {
        super.setBaseDao(productCategoryDao);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findRoots() {
        return productCategoryDao.findRoots(null);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findRoots(Integer count) {
        return productCategoryDao.findRoots(count);
    }

    @Transactional(readOnly = true)
    @Cacheable("productCategory")
    public List<ProductCategory> findRoots(Integer count, String cacheRegion) {
        return productCategoryDao.findRoots(count);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findParents(ProductCategory productCategory) {
        return productCategoryDao.findParents(productCategory, null);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findParents(ProductCategory productCategory, Integer count) {
        return productCategoryDao.findParents(productCategory, count);
    }

    @Transactional(readOnly = true)
    @Cacheable("productCategory")
    public List<ProductCategory> findParents(ProductCategory productCategory, Integer count, String cacheRegion) {
        return productCategoryDao.findParents(productCategory, count);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findTree() {
        return productCategoryDao.findChildren(null, null);
    }

    @Override
    public List<ProductCategory> findListByName(String productName) {
        return productCategoryDao.findChildren(productName);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findChildren(ProductCategory productCategory) {
        return productCategoryDao.findChildren(productCategory, null);
    }

    @Transactional(readOnly = true)
    public List<ProductCategory> findChildren(ProductCategory productCategory, Integer count) {
        return productCategoryDao.findChildren(productCategory, count);
    }

    @Transactional(readOnly = true)
    @Cacheable("productCategory")
    public List<ProductCategory> findChildren(ProductCategory productCategory, Integer count, String cacheRegion) {
        return productCategoryDao.findChildren(productCategory, count);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void save(ProductCategory productCategory) {
        super.save(productCategory);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public ProductCategory update(ProductCategory productCategory) {
        return super.update(productCategory);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public ProductCategory update(ProductCategory productCategory, String... ignoreProperties) {
        return super.update(productCategory, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(Long... ids) {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(ProductCategory productCategory) {
        super.delete(productCategory);
    }

    @Transactional(readOnly = true)
    @Override
    public JSONArray buildProductCategoryTree() {
        List<ProductCategory> aList = null;
        try {
            aList = findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* 构建树 */
        JSONArray array = new JSONArray();
        JSONObject tree = new JSONObject();
        tree.put("id", 1);
        tree.put("pId", 0);
        tree.put("name", "商品分类");
        tree.put("open", true);
        array.put(tree);
        for (ProductCategory a : aList) {
            JSONObject child = new JSONObject();
            child.put("id", a.getId());
            child.put("pId", 1);
            child.put("name", a.getName());
            child.put("open", false);
            array.put(child);
        }
        return array;

    }

    @Override
    public List<ProductCategory> findTopProductCategory() {
        List<Filter> fs = new ArrayList<>();
        fs.add(Filter.eq("isTop", true));
        return findList(8, fs, null);
    }

}